// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================

package tc.common.symbology;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.util.Vector;

public class SymbolItemIregular extends SymbolItem{
	public enum Type {LINE, LINE_CLOSED, AREA}
	
	public Type type;
	public Vector<Point> points = new Vector<Point>();
		
	public SymbolItemIregular(Type type, Color color) {
		super(color);
		this.type = type;
	}
	
	private void drawFinal(Graphics g, int[] xps, int[] yps) {
		switch (type) {
		case LINE:
			Graphics2D g2 = (Graphics2D)g;
			Stroke oldStroke = g2.getStroke();
			g2.setStroke(new BasicStroke(2.0F));
			
			g.drawPolyline(xps, yps, xps.length);
			
			g2.setStroke(oldStroke);
			break;
		case LINE_CLOSED:
			g.drawPolygon(xps, yps, xps.length);
			break;
		case AREA:
			g.fillPolygon(xps, yps, xps.length);
			break;
		}
	}
	
	@Override public void display(Graphics g, int x, int y, int kx, int ky ) {
		g.setColor(color);
		int[] xps = new int[points.size()];
		int[] yps = new int[points.size()];
		
		for (int i = 0; i < points.size(); i++) {
			xps[i] = x + points.get(i).x/kx;
			yps[i] = y + points.get(i).y/ky;	
		}
		
		//g.drawPolygon(xps, yps, points.size());
		drawFinal(g, xps, yps);
	}
	
	@Override public void displayRot(Graphics g, int x, int y, int ox, int oy, int kx, int ky, int dirx, int diry ) {
		AffineTransform transform = AffineTransform.getRotateInstance(dirx, diry, ox, oy );
		
		g.setColor(color);
		int[] xps = new int[points.size()];
		int[] yps = new int[points.size()];
		
		for (int i = 0; i < points.size(); i++) {
			Point pt = new Point(points.get(i).x/kx, points.get(i).y/ky );
			Point pt2 = new Point();
			transform.transform(pt, pt2);
			
			xps[i] = pt2.x + x - ox;
			yps[i] = pt2.y + y - oy;
		}
		
		//g.drawPolygon(xps, yps, points.size());
		drawFinal(g, xps, yps);
	}
}
